﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZooKeeperManage.Dispersed;

namespace ZooKeeperManage
{
    public class WorkClient
    {
        /// <summary>
        /// 服务路径
        /// </summary>
        private readonly string _serverPath;
        private string master;
        /// <summary>
        /// Master节点对应zookeeper中的节点路径
        /// </summary>
        private readonly string _masterPath;

        private readonly ZooKeeperService _zkService;
        /// <summary>
        /// 记录集群中Master节点的基本信息
        /// </summary>
        private WorkData _masterData;
        /// <summary>
        /// 序列化
        /// </summary>
        private readonly ISerialize _serializ;

        public event Action<WorkData> ServerDataEven;
        public WorkClient(WorkData rd, string serverPath = "/Server", string masterPath = "master", ISerialize serialize = null)
        {
            _masterData = rd; // 记录服务器基本信息
            _serverPath = serverPath + "/" + _masterData.Name;
            //  ss = serverPath + "/" + masterPath ;
            _masterPath = serverPath + "/" + _masterData.Name + "/" + masterPath;
            master = masterPath;
            _zkService = new ZooKeeperService();
            if (serialize == null) _serializ = new SerializeJson();
            try
            {
                // 订阅Master节点删除事件
                _zkService.SubscribeDataChanged(_masterPath);
                _zkService.DataChanged += _zkService_DataChanged;
                _masterData = _serializ.Deserialize<WorkData>(_zkService.GetData(_masterPath));
            }
            catch (Exception ex)
            {
                //if (!_serverPath.EndsWith("/"))
                //    _serverPath += "/";
                // 订阅Master节点删除事件
                _zkService.ChildrenChanged += _zkService_ChildrenChanged;
                _zkService.SubscribeChildrenChanged(_serverPath);
            }

        }

        private void _zkService_ChildrenChanged(List<string> obj)
        {
            bool state = false;
            foreach (var item in obj)
            {
                if (item.Equals(master))
                    state = true;
            }
            if (state)
            {
                // 订阅Master节点删除事件
                _zkService.SubscribeDataChanged(_masterPath);
                _zkService.DataChanged += _zkService_DataChanged;
                _masterData = _serializ.Deserialize<WorkData>(_zkService.GetData(_masterPath));
                _zkService.ChildrenChanged -= _zkService_ChildrenChanged;

                _masterData = _serializ.Deserialize<WorkData>(_zkService.GetData(_masterPath));
                ServerDataEven?.Invoke(_masterData);
            }
        }

        private void _zkService_DataChanged(string obj)
        {
            _masterData = _serializ.Deserialize<WorkData>(_zkService.GetData(_masterPath));
        }

        public WorkData GetWork()
        {
            return _masterData;
        }
    }
}
